<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" class="login-box">
      <h3 class="login-title">欢迎登录三湘后台管理系统</h3>
      <el-form-item label="账号" prop="name">
        <el-input type="text" placeholder="请输入用户名" v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" placeholder="请输入密码" v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(form)">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import adminApi from "@/api/admin/login";
import cookie from "js-cookie";
  export default {
      layout: "sign",
    data() {
      return {
        form: {
          name: '',
          password: ''
        },
        rules: {
          name: [{
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          }],
          password: [{
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      submitForm(formName) {
        adminApi.submitLogin(formName).then(response=>{
          console.log(response)
        if (response.data.code == 200) {
          this.$message({
            type: "success",
            message: "登录成功",
          });
          //登录成功把token存在cookie中
          cookie.set("atoken", response.data.data.atoken, {
           
          });
          //跳转页面
          this.$router.push({path: '/manage'})
        }else{
          this.$message.error(response.data.message);
        }
        })
         
      }
    }
  }
</script>

<style  scoped>
  .login-box {
    width: 350px;
    margin: 120px auto;
    border: 1px solid #DCDFE6;
    padding: 20px;
    border-radius: 5px;
    box-shadow: 0 0 30px #DCDFE6;
  }

  .login-title {
    text-align: center;
  }
</style>

